package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.DishDto;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavor;
import com.itheima.mapper.DishMapper;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: guozy
 * @Date: 2022/7/31 - 07 - 31 - 9:47
 * @Description: com.itheima.service.impl
 * @version: 1.0
 */
@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品,同时保存对应口味数据
     * @param dishDto
     */
    public void saveDishwithDishFlavor(DishDto dishDto) {
        //保存基本信息到菜品表
        this.save(dishDto);
        //保存信息到菜品口味表,出现问题 dishId未封装上
        //从dishDto取出id
        Long id = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item)-> {
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());
      dishFlavorService.saveBatch(flavors);
    }

    /**
     * 通过id查询dish和dishFlavor
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {

        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();

        BeanUtils.copyProperties(dish,dishDto);
        // 查询当前菜品对应的口味信息通过id
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(list);

        return dishDto;
    }

    /**
     * 编辑更新dish表,
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateDishwithDishFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);

        //清理当前菜品对应口味的数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();

        //通过唯一id删除,指定到对象,DishFlavor中菜品id,对应dishDto中的id
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        //调用方法移除
        dishFlavorService.remove(queryWrapper);

        //添加提交过来的口味数据-------dish_flavor的insert操作
        //从dishDto取出id
        Long id = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();

        // 健壮性判断
        if (flavors!=null) {
            flavors = flavors.stream().map((item)-> {
                item.setDishId(id);
                return item;
            }).collect(Collectors.toList());
        }


        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 通过ids数组删除dish表和dish口味表
     * @param ids
     */
    @Override
    public void delectDishwithDishFlavor(List<Long> ids) {
        // 删除dish表
        this.removeByIds(ids);
        // 删除口味表,需要获得口味表中dishFlavor的dish id
        dishFlavorService.removeByIds(ids);
    }
}
